<?php
    /**
     * P2C Admin projects panel.
     *
     * @package WordPress
     * @subpackage Administration
     */

    if ( ! current_user_can( 'manage_options' ) )
        wp_die( __( 'You do not have sufficient permissions to manage options for this site.' ) );

    $action = p2c_current_action();
    $saved = -1;
	if ( 'save' == $action ) {
		$id = $_POST['pid'];
        
        $p2c_project = new p2c_project( $id );
        $p2c_project->name = trim( $_POST['p2cpr-name'] );
        $p2c_project->image = trim( $_POST['p2cpr-image'] );
        $p2c_project->url = trim( $_POST['p2cpr-url'] );
        $p2c_project->external_id = trim( $_POST['p2cpr-externalid'] );
        $p2c_project->active =  $_POST['p2cpr-active'] ;
        $p2c_project->currency = $_POST['p2cpr-currency'] ;

		$saved = $p2c_project->save();
	}

    $list_table = new P2C_Project_List_Table();
    $list_table->prepare_items();

	$_GET['pid'] = isset( $_GET['pid'] ) ? $_GET['pid'] : '';

	$p2cpr = p2c_project( $_GET['pid'] );
    $prid = $p2cpr->initial ? -1 : $p2cpr->id;

?>
<div class="wrap">
<?php screen_icon(); ?>

<h2><?php
        if('edit' == $action) {
            echo esc_html( __( 'Edit Project', 'p2c_admin' ) );
        } else {
            echo esc_html( __( 'Projects', 'p2c_admin' ) );
        }
    
?></h2>
<?php if($saved>-1) echo '<div id="message" class="updated"><p>Project saved</p></div>'; 

if('edit' == $action) {
?>
<form method="post" action="<?php echo esc_url( add_query_arg( array( 'pid' => $pid ), menu_page_url( 'p2cpr', false ) ) ); ?>" id="p2cpr-form-element">
    <input type="hidden" id="pid" name="pid" value="<?php echo (int) $prid; ?>" />
	<input type="hidden" id="hiddenaction" name="action" value="save" />

    <table class="form-table">
    <tr valign="top">
    <th scope="row"><label for="p2cpr-name">Project name</label></th>
    <td><input type="text" id="p2cpr-name" name="p2cpr-name" class="regular-text" value="<?php echo esc_attr( $p2cpr->name ); ?>" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpr-externalid">External ID</label></th>
    <td><input type="text" id="p2cpr-externalid" name="p2cpr-externalid" class="regular-text" value="<?php echo esc_attr( $p2cpr->external_id ); ?>" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpr-url">(Affiliate) link</label></th>
    <td><input type="text" id="p2cpr-url" name="p2cpr-url" class="regular-text" value="<?php echo esc_attr( $p2cpr->url ); ?>" style="width: 100%;" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpr-image">Logo</label></th>
    <td><input type="text" id="p2cpr-image" name="p2cpr-image" class="regular-text" value="<?php echo esc_attr( $p2cpr->image ); ?>" style="width: 100%;" /></td>
    </tr>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpr-currency">Currency</label></th>
    <td><input type="text" id="p2cpr-currency" name="p2cpr-currency" class="regular-text" value="<?php echo esc_attr( $p2cpr->currency ); ?>" style="width: 100%;" /></td>
    </tr>
    <tr valign="top">
    <th scope="row">Active</th>
    <td>
        <fieldset><legend class="screen-reader-text"><span>Active project</span></legend>
        <label title='Yes'><input type='radio' name='p2cpr-active' value='1' <?php if($p2cpr->active) echo "checked='checked'"; ?> />Yes</label><br />
        <label title='No'><input type='radio' name='p2cpr-active' value='0' <?php if(!$p2cpr->active) echo "checked='checked'"; ?> />No</label><br />
        </fieldset>
    </td>
    </tr>
	</table>

    <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"  /></p>
</form>
<h3>Project Data</h3>
<?php
        $data_table = new P2C_ProjectData_List_Table();      
        $data_table->prepare_items($prid);
        $data_table->display();
} else {
?>
<form method="get" action="">
    <input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ); ?>" />
	<?php $list_table->display(); ?>
</form>
<?php
}
?>

</div>